// pages/house-keep/index.js
import Config from '../../etc/config'
var app = getApp()
const {
  $Toast
} = require('../../components/dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    right: '../../images/icon/right.png',
    addId: '',
    type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.typeId = options.typeId
    this.data.type = options.type
    if (wx.getStorageSync('userId')) {
      this.getAdImg(options.type)
    } else {
      wx.navigateTo({
        url: '../authorize/index',
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.addId !== '') {
      this.getAddrById()
    }
    this.getAddLength()
    // this.getAdImg(this.data.type)
  },

  // 获取用户地址数量
  getAddLength() {
    let params = {
      userId: wx.getStorageSync('userId'),
    }
    let token = wx.getStorageSync("token");
    Config._post('/useraddress/userAddressIsExistByUserId', token, params).then(res => {
      if (res.data) {
        this.setData({
          msg: '选择收货地址',
          handleWay: 'selectAddress'
        })
      } else {
        this.setData({
          msg: '增加收货地址',
          handleWay: 'addAddress'
        })
      }
    })
  },

  // 获取默认地址和页面广告图
  getAdImg(type) {
    let params = {
      userId: wx.getStorageSync('userId'),
      typeId: type
    }
    let token = wx.getStorageSync("token");
    Config._post('/merServicesType/getImgStrByTypeId', token, params).then(res => {
      if (res.data.userAddress){
        this.setData({
          addressInfo: res.data.userAddress,
          addId: res.data.userAddress.id
        })
        this.getShop()
      }
      if (res.data.path){
        this.setData({
          img: res.data.path
        })
      }
    })
  },

  // 选择地址
  selectAddress() {
    wx.navigateTo({
      url: '../address/index?typeId=' + this.data.typeId,
    })
  },

  // 添加地址
  addAddress() {
    wx.navigateTo({
      url: '../add-address/index',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  // 修改地址
  editAddess(e) {
    wx.navigateTo({
      url: '../add-address/index?id=' + e.currentTarget.dataset.id,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  // 修改会员卡号
  cardChange(e) {
    var cardNumber = 'addressInfo.member'
    this.data.addressInfo.member = e.detail.value
    this.setData({
      cardNumber: e.detail.value
    })
  },

  // 根据地址ID获取地址详情
  getAddrById() {
    let params = {
      id: this.data.addId
    }
    let token = wx.getStorageSync("token");
    Config._post('/useraddress/getAddrById', token, params).then(res => {
      this.setData({
        addressInfo: res.data
      })
      this.getShop()
    })
  },

  // 获取门店
  getShop() {
    let params = {
      merSerTypeId: this.data.typeId,
      addressId: this.data.addId
    }
    let token = wx.getStorageSync("token")
    Config._post('/merServicesType/getMerSerTypeDetail', token, params).then(res => {
      wx.setNavigationBarTitle({
        title: res.data.name
      })
      if (res.code === 0) {
        this.setData({
          shopId: res.data.shopId
        })
      }
    })
  },

  valueChange(e) {
    let value = e.detail.value
    this.setData({
      buyerRemake: value
    })
  },

  // 下单
  orderSubmit: function(e) {
    this.setData({
      disabled: true
    })
    wx.showLoading({
      // title: '',
    })
    if (!this.data.buyerRemake){
      $Toast({
        content: '请填写服务内容'
      });
      wx.hideLoading()
      this.setData({
        disabled: false
      })
      return
    }
    let formId = e.detail.formId
    let params = {
      userId: wx.getStorageSync('userId'),
      merTypeId: this.data.typeId,
      buyerRemake: this.data.buyerRemake,
      shopId: this.data.shopId,
      addressId: this.data.addId,
      formId: formId
    }
    let token = wx.getStorageSync("token");
    Config._post('/order/submitOrder', token, params).then(res => {
      wx.hideLoading()
      this.setData({
        disabled: false
      })
      wx.reLaunch({
        url: '../success-msg/index',
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // title: wx.getStorageSync('mallName') + '——' + app.globalData.shareProfile,
      title: app.globalData.shareTitle,
      path: '/pages/house-keep/index?type=' + this.data.type + '&typeId=' + this.data.typeId,
      imageUrl: this.data.img,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})